package com.web;

import com.bean.Phone;
import com.dao.PhoneDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/phone/two")
public class PhoneTwoVagueServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String condition1 = req.getParameter("condition1");
        String condition2 = req.getParameter("condition2");

        if (condition1.length() == 0 || condition2.length() == 0) {
            req.setAttribute("message", "请先输入价格再查询哦！");
            req.getRequestDispatcher("/phone/message.jsp").forward(req, resp);
            return;
        }

        try {
            List<Phone> phones = null;
            phones = new PhoneDao().vague(Float.parseFloat(condition1), Float.parseFloat(condition2));

            req.setAttribute("iphone", phones);
            req.getRequestDispatcher("/phone/phoneList.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("message", "查询失败！");
            req.getRequestDispatcher("/phone/message.jsp").forward(req, resp);
        }
    }
}
